package com.dstz.demo.rest.controller;

import com.dstz.base.api.aop.annotion.CatchErr;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.rest.BaseController;
import com.dstz.demo.core.manager.AmaterialversionManager;
import com.dstz.demo.core.manager.VuinfoManager;
import com.dstz.demo.core.model.Amaterialversion;
import com.dstz.demo.core.model.Vuinfo;
import com.dstz.demo.core.model.Vuinfos;
import com.dstz.sys.util.ContextUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/sys/amaterialversion")
public class AmaterialversionController extends BaseController<Amaterialversion> {

    @Override
    protected String getModelDesc() {
        return null;
    }
    @Resource
    AmaterialversionManager amaterialversionManager;
    @Resource
    VuinfoManager vuinfoManager;

    @PostMapping("/save/{id}")
    @CatchErr(write2response = true,value = "查询信息失败")
    public ResultMsg saves(@PathVariable("id") String id, HttpServletRequest request){
        String[] ids=id.split(",");
        for (int i=0;i<ids.length;i++){
            Integer count=amaterialversionManager.counts(ids[i]);
            if(count==0) {
                Amaterialversion amaterialversion = new Amaterialversion();
                amaterialversion.setMaterialid(ids[i]);
                amaterialversion.setUserid(ContextUtil.getCurrentUserId());
                amaterialversion.setProjectid("1");
                amaterialversionManager.save(amaterialversion);
                Vuinfo vuinfo = new Vuinfo();
                vuinfo.setShoucangid(ids[i]);
                vuinfo.setProjectid("1");
                vuinfo.setUserid(ContextUtil.getCurrentUserId());
                vuinfoManager.save(vuinfo);
            }
            continue;
        }
        return getSuccessResult("收藏成功");
    }



    @PostMapping("/determine/{id}")
    @CatchErr(write2response = true,value = "查询信息失败")
    public ResultMsg determine(@PathVariable("id") String id){
        String[] ids=id.split(",");
        Vuinfo vuinfo=new Vuinfo();
        vuinfo.setId(ids[0]);
        vuinfo.setDuibiid(ids[1]);
        vuinfoManager.updates(vuinfo);
        return getSuccessResult("提交成功");
    }

    @PostMapping("/lists")
    @CatchErr(write2response = true,value = "查询信息失败")
    public List<Vuinfos> lists() {
        List<Vuinfos> list=vuinfoManager.lists(ContextUtil.getCurrentUserId());
        if(list!=null){
            for(int i=0;i<list.size();i++){
                Vuinfos vl=list.get(i);
                if(vl.getIdlv()==null){
                    vl.setIdlv("false");
                }
                if(vl.getShoucangid()==null){
                    vl.setShoucangid("false");
                }
                if(vl.getWlbm()==null){
                    vl.setWlbm("false");
                }
                if(vl.getCpmc()==null){
                    vl.setCpmc("false");
                }
                if(vl.getScj()==null){
                    vl.setScj("false");
                }
                if(vl.getScjs()==null){
                    vl.setScjs("false");
                }
                if(vl.getGcjg1()==null){
                    vl.setGcjg1("false");
                }
                if(vl.getGcjg2()==null){
                    vl.setGcjg2("false");
                }
                if(vl.getGcjg3()==null){
                    vl.setGcjg3("false");
                }
                if(vl.getDuibiid()==null){
                    vl.setDuibiid("false");
                }
                if(vl.getWlbms()==null){
                    vl.setWlbms("false");
                }
                if(vl.getAscpmc()==null){
                    vl.setAscpmc("false");
                }
                if(vl.getGcjg1s()==null){
                    vl.setGcjg1s("false");
                }
                if(vl.getGcjg2s()==null){
                    vl.setGcjg2s("false");
                }
                if(vl.getGcjg3s()==null){
                    vl.setGcjg3s("false");
                }
            }
        }
        return list;
    }

    @PostMapping("/deletes/{id}")
    @CatchErr(write2response = true,value = "查询信息失败")
    public ResultMsg deletes(@PathVariable("id") String id){
        Integer num=vuinfoManager.deletes(id);
        Integer nums=amaterialversionManager.deleteById(id);
        if(num>0){
            return getSuccessResult("删除成功");
        }else{
            return getSuccessResult("删除失败");
        }
    }
}
